// package-activity/pages/activity-copy-list/index.js
import { ManageTargetInfoUpdateMixins, PageScrollListMixins } from '../../../behaviors/page';
import { PACKAGE_ACTIVITY_PAGE, PACKAGE_MARKET_PAGE } from '../../../constants/route';
import { navTo, showLoading, hideLoading } from '../../../common/env';
import { ACTIVITY_TYPE } from '../../../constants/common';
import { ACTIVITY_OWNER_VALUE } from '../../../constants/activity';
import { getCopyActivityList } from '../../../api/brd';

Page({
    mixins: [ManageTargetInfoUpdateMixins, PageScrollListMixins],
    data: {
    },
    onLoad(options) {
    },
    async _handleGetList(pageNo) {
        !this.data.loaded && showLoading('加载中...');
        const { pageData, keyword, manageTargetInfo } = this.data;
        let { ts } = pageData;
        pageNo === 1 && (ts = Date.now());
        const { rows = [], total } = await getCopyActivityList({ pageNo, ts, keyword }, {
            owner: ACTIVITY_OWNER_VALUE.brand,
            ownerName: manageTargetInfo.name,
            ownerPic: manageTargetInfo.pic,
            canManage: true,
        });
        this.setData({
            pageData: {
                ...pageData,
                refresh: false,
                ts,
                pageNo,
                total,
                rows: pageNo == 1 ? rows : [...pageData.rows, ...rows],
            },
        });
        if (!this.data.loaded) hideLoading(); this.setData({ loaded: true });
    },
    handleNavToCopy(e) {
        const { id: copySourceId, type } = e.detail;
        if (type === ACTIVITY_TYPE.group_buying) {
            navTo(PACKAGE_ACTIVITY_PAGE['activityEdit'], {
                copySourceId,
            });
        } else if (type === ACTIVITY_TYPE.lottery) {
            navTo(PACKAGE_MARKET_PAGE['lotteryEdit'], {
                copySourceId,
            });
        }
    },
})
